package com.rzico.sbl.ui.statistic

import android.widget.ImageView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityFundBillBinding
import com.rzico.sbl.model.OrderFooter
import com.rzico.sbl.model.OrderGoods
import com.rzico.sbl.model.OrderHeader
import com.rzico.sbl.other.*
import com.rzico.sbl.ui.order.OrderDetailActivity
import com.rzico.sbl.viewmodel.StatisticFundViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common_ui.base.*
import com.xinnuo.common_ui.glideExt.loadImage
import com.xinnuo.common_ui.utils.setEmptyImage
import com.xinnuo.common_ui.utils.showTimeRangeDialog
import com.xinnuo.slimadapter.SlimAdapter

class FundBillActivity : BaseActivity(R.layout.activity_fund_bill) {

    private val mBinding by lazy { ActivityFundBillBinding.bind(getRootView()) }

    private var mOrderPay = ""
    private var mBeginDate = getTodayDate()
    private var mEndDate = getTodayDate()

    override fun getViewModel() = get<StatisticFundViewModel>()

    override fun initData() {
        initTitle(title = "记账收款")

        initLayout()

        mBinding.listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.billBottom.gone()

        mBinding
            .listLayout
            .apply {
                emptyLayout.also {
                    it.emptyImg.setEmptyImage(
                        resId = R.mipmap.icon_order_no,
                        dpSize = 100f
                    )
                    it.emptyHint.text = "暂无相关记账收款信息！"
                }

                recycleList.also { layout ->
                    layout.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData(0) },
                        onLoad = {
                            isLoadingMore
                                .conditionFalse {
                                    isLoadingMore = true
                                    getData(pageNum)
                                }
                        }
                    )

                    mAdapter = SlimAdapter.creator()
                        .register<OrderHeader>(R.layout.item_statistic_order_title) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_order_time, bean.hint + bean.time)
                                text(R.id.item_order_status, bean.status_hint)

                                clicked {
                                    startActivityEx<OrderDetailActivity>("orderSn" to bean.orderSN)
                                }
                            }
                        }
                        .register<OrderGoods>(R.layout.item_statistic_order_content) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_order_title, "${bean.consignee}  ${bean.phone}")
                                text(R.id.item_order_name, bean.name)
                                text(R.id.item_order_price, "单价：${bean.price.formatDecimal()} 元")
                                text(R.id.item_order_num, "x${bean.quantity}")

                                getView<ImageView>(R.id.item_order_img)
                                    .loadImage(bean.thumbnail)

                                with<ImageView>(R.id.item_order_img_type) {
                                    when (bean.methodDescr) {
                                        "offline" -> {
                                            it.visible()
                                            it.setImageResource(R.mipmap.icon_order03)
                                        }

                                        "eleme", "elemengc" -> {
                                            it.visible()
                                            it.setImageResource(R.mipmap.icon_order04)
                                        }

                                        "meituan" -> {
                                            it.visible()
                                            it.setImageResource(R.mipmap.icon_order05)
                                        }

                                        "nongfu" -> {
                                            it.visible()
                                            it.setImageResource(R.mipmap.icon_order06)
                                        }

                                        "jd" -> {
                                            it.visible()
                                            it.setImageResource(R.mipmap.icon_order10)
                                        }

                                        "taobao" -> {
                                            it.visible()
                                            it.setImageResource(R.mipmap.icon_order11)
                                        }

                                        "pinduoduo" -> {
                                            it.visible()
                                            it.setImageResource(R.mipmap.icon_order12)
                                        }

                                        "dummy" -> {
                                            it.visible()
                                            it.setImageResource(R.mipmap.icon_order22)
                                        }

                                        "ali" -> {
                                            it.visible()
                                            it.setImageResource(R.mipmap.icon_order26)
                                        }

                                        else -> it.gone()
                                    }
                                }

                                clicked {
                                    startActivityEx<OrderDetailActivity>("orderSn" to bean.orderSN)
                                }
                            }
                        }
                        .register<OrderFooter>(R.layout.item_statistic_order_foot) { jector, bean, _ ->
                            jector.apply {
                                text(R.id.item_order_count, "共 ${bean.total_num} 件商品")
                                text(R.id.item_order_total, bean.amount.formatDecimal())

                                clicked {
                                    startActivityEx<OrderDetailActivity>("orderSn" to bean.orderSN)
                                }
                            }
                        }
                        .attachTo(layout)
                }
            }
    }

    override fun initListener() {
        mBinding.apply {
            billTypeLl
                .oneClick {
                    getViewModel()
                        .showDropFilter(
                            getIContext(),
                            billTop,
                            getViewModel().mMonthList,
                            billType,
                            billTypeArrow
                        ) {
                            billType.setTextColor(
                                getColorEx(
                                    conditionIf(
                                        it.id.isEmpty(),
                                        R.color.black,
                                        R.color.colorAccent
                                    )
                                )
                            )
                            billTypeArrow.setImageResource(
                                conditionIf(
                                    it.id.isEmpty(),
                                    R.mipmap.icon_member01,
                                    R.mipmap.icon_member02
                                )
                            )

                            mOrderPay = it.id
                            updateList()
                        }
                }

            billTimeLl
                .oneClick {
                    getViewModel()
                        .showDropTimeFilter(
                            getIContext(),
                            billTime,
                            billTimeArrow,
                            billTop,
                            getViewModel().mTimeList
                        ) { bean ->

                            (bean.id != "7")
                                .condition {
                                    getViewModel()
                                        .mTimeList
                                        .last()
                                        .name = "自定义时间"
                                }

                            when (bean.id) {
                                "1" -> {
                                    mBeginDate = getTodayDate()
                                    mEndDate = getTodayDate()

                                    updateList()
                                }

                                "2" -> {
                                    mBeginDate = getBeforeDate()
                                    mEndDate = getBeforeDate()

                                    updateList()
                                }

                                "3" -> {
                                    mBeginDate = getMondayDate()
                                    mEndDate = getSundayDate()

                                    updateList()
                                }

                                "4" -> {
                                    mBeginDate = getFirstDate()
                                    mEndDate = getEndDate()

                                    updateList()
                                }

                                "5" -> {
                                    mBeginDate = getBeforeFirstDate()
                                    mEndDate = getBeforeEndDate()

                                    updateList()
                                }

                                "6" -> {
                                    mBeginDate = getYearFirstDate()
                                    mEndDate = getYearEndDate()

                                    updateList()
                                }

                                "7" -> {
                                    showTimeRangeDialog(
                                        isCurrentDate = true,
                                        isAfterLimited = true,
                                        onResult = { start, end ->
                                            getViewModel()
                                                .mTimeList
                                                .forEach { it.isChecked = false }

                                            getViewModel()
                                                .mTimeList
                                                .last()
                                                .isChecked = true

                                            billTime.text = "自定义"

                                            mBeginDate = start
                                            mEndDate = end
                                            bean.name = "$mBeginDate  ~  $mEndDate"

                                            updateList()
                                        }
                                    )
                                }
                            }
                        }
                }
        }
    }

    /**
     * 记账订单列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .orderBill(
                page = index,
                type = mOrderPay,
                beginDate = mBeginDate,
                endDate = mEndDate,
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        mList.notEmpty {
            clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}